package com.itcast.util;

import com.itcast.pojo.PageBean;

/**
 * 分页工具类
 * @author Administrator
 *
 */
public class PageUtil {


	public static String genPagination(String targetUrl,long totalNum,int currentPage,int pageSize,String param){
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li><a href='"+targetUrl+"?page=1&"+param+"'>首页</a></li>");
			if(currentPage>1){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage-1)+"&"+param+"'>上一页</a></li>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li>"+i+"</li>");
				}else{
					pageCode.append("<li><a href='"+targetUrl+"?page="+i+"&"+param+"'>"+i+"</a></li>");
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage+1)+"&"+param+"'>下一页</a></li>");
			}
			pageCode.append("<li><a href='"+targetUrl+"?page="+totalPage+"&"+param+"'>尾页</a></li>");
			return pageCode.toString();
		}
	}

	public static String genPaginationNoParam(String targetUrl,long totalNum,int currentPage,int pageSize){
		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li><a href='"+targetUrl+"?page=1'>首页</a></li>");
			if(currentPage>1){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage-1)+"'>上一页</a></li>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li>"+i+"</li>");
				}else{
					pageCode.append("<li><a href='"+targetUrl+"?page="+i+"'>"+i+"</a></li>");
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage+1)+"'>下一页</a></li>");
			}
			pageCode.append("<li><a href='"+targetUrl+"?page="+totalPage+"'>尾页</a></li>");
			return pageCode.toString();
		}
	}

	public static String genPaginationWithForm(PageBean pageBean){
		int totalNum=pageBean.getTotalCount();
		int pageSize=pageBean.getPageSize();
		int currentPage=pageBean.getCurrPageNo();

		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li><a href='javascript:goToPage(1)'>首页</a></li>");
			if(currentPage>1){
				pageCode.append("<li><a href='javascript:goToPage("+(currentPage-1)+")'>上一页</a></li>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li class='curr'>"+i+"</li>");
				}else{
					pageCode.append("<li><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li><a href='javascript:goToPage("+(currentPage+1)+")'>下一页</a></li>");
			}
			pageCode.append("<li><a href='javascript:goToPage("+totalPage+")'>尾页</a></li>");
			pageCode.append("<br />");
			pageCode.append("<script type='text/javascript'>");
			pageCode.append("     function goToPage(pageIndex){");
			pageCode.append("       var at=$('form').attr('action');");
			pageCode.append("           at=at+'?pageIndex='+pageIndex;");
			pageCode.append("          $('form').attr('action',at);");
			pageCode.append("              $('form').submit();");
			pageCode.append("}");
			pageCode.append("</script>");


			pageCode.append("<style type='text/css'>");

			pageCode.append("</style>");


			return pageCode.toString();
		}
	}

	public static String genPaginationWithForm2(PageBean pageBean){
		int totalNum=pageBean.getTotalCount();
		int pageSize=pageBean.getPageSize();
		int currentPage=pageBean.getCurrPageNo();

		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li><a href='javascript:goToPage(1)'>首页</a></li>");
			if(currentPage>1){
				pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+(currentPage-1)+")'><span class='pagepre'></span></a></li>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
				}else{
					pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+currentPage+1+")'><span class='pagenxt'></span></a></li>");
			}
			/*pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+totalPage+")'<span class='pagenxt'></span></a></li>");*/
			pageCode.append("<br />");
			pageCode.append("<script type='text/javascript'>");
			pageCode.append("     function goToPage(pageIndex){");
			pageCode.append("       var at=$('form').attr('action');");
			pageCode.append("           at=at+'?pageIndex='+pageIndex;");
			pageCode.append("          $('form').attr('action',at);");
			pageCode.append("              $('form').submit();");
			pageCode.append("}");
			pageCode.append("</script>");
			return pageCode.toString();
		}
	}


	public static String genPaginationWithFormPc(PageBean pageBean){
		int totalNum=pageBean.getTotalCount();
		int pageSize=pageBean.getPageSize();
		int currentPage=pageBean.getCurrPageNo();

		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		if(totalPage==0){
			return "未查询到数据";
		}else{
			StringBuffer pageCode=new StringBuffer();
			pageCode.append("<li class='disabled'><a href='javascript:goToPage(1)'>&laquo;</a></li>");
			if(currentPage>1){
				pageCode.append("<li class=''><a href='javascript:goToPage("+(currentPage-1)+")'>&laquo;</a></li>");
			}
			for(int i=currentPage-2;i<=currentPage+2;i++){
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					pageCode.append("<li class='page_ac'><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
				}else{
					pageCode.append("<li class='page_ac'><a href='javascript:goToPage("+i+")'>"+i+"</a></li>");
				}
			}
			if(currentPage<totalPage){
				pageCode.append("<li class=''><a href='javascript:goToPage("+currentPage+1+")'>&raquo;</a></li>");
			}
			/*pageCode.append("<li class='paginItem'><a href='javascript:goToPage("+totalPage+")'<span class='pagenxt'></span></a></li>");*/
			pageCode.append("<br />");
			pageCode.append("<script type='text/javascript'>");
			pageCode.append("     function goToPage(pageIndex){");
			pageCode.append("       var at=$('form').attr('action');");
			pageCode.append("           at=at+'?pageIndex='+pageIndex;");
			pageCode.append("          $('form').attr('action',at);");
			pageCode.append("              $('form').submit();");
			pageCode.append("}");
			pageCode.append("</script>");
			return pageCode.toString();
		}
	}

}